import {
  personalMan,
  getCity,
  uploadMerchant,
  bankList,
  bankBranchInfo,
  getProvinces,
} from '../../api.js'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pic: 'http://document.growthupup.com/add_pic.png',
    showProvince: false,
    merFullName: '', //商户全称
    merShortName: '', //商户品牌名称/简称  
    merCertNo: '', //营业执照号
    legalIdCard: '', //法人身份证号
    legalName: '', //法人姓名
    merAddress: '', //商户经营地址所在具体地址
    merCity: '', //商户经营地址所在市
    merDistrict: '', //商户经营地址所在区
    merProvince: '', //商户经营地址所在省
    cardNo: '', //结算银行账号或者银行卡号
    headBankCode: '', //开户银行总行编码
    bankCity: '', //开户市
    bankCode: '', //开户银行支行编码
    bankProvince: '', //开户省
    IDCARD_FRONT: {
      url: 'http://document.growthupup.com/add_pic.png',
      _file: '',
      thumb: ''
    }, //法人身份证正面
    IDCARD_BACK: {
      url: 'http://document.growthupup.com/add_pic.png',
      _file: '',
      thumb: ''
    }, //法人身份证反面
    SETTLE_BANKCARD: {
      url: 'http://document.growthupup.com/add_pic.png',
      _file: '',
      thumb: ''
    }, //结算银行卡
    HAND_IDCARD: {
      url: 'http://document.growthupup.com/add_pic.png',
      _file: '',
      thumb: ''
    }, //手持身份证
    HAND_BANKCARD: {
      url: 'http://document.growthupup.com/add_pic.png',
      _file: '',
      thumb: ''
    }, //手持银行卡
    UNI_CREDIT_CODE: {
      url: 'http://document.growthupup.com/add_pic.png',
      _file: '',
      thumb: ''
    },
    merLegalPhone: '', //商户联系人手机
    // merLegalEmail: '', //商户联系人邮箱
    province: [], //三联动
    city: [],
    area: [],
    index: -1,
    index2: -1,
    index3: -1,
    province_two: [], //二联动
    city_two: [],
    index_two: -1,
    index2_two: -1,
    totalBank_index: -1, //总行
    totalBank: [],
    branchBank: [], //支行
    branchBank_index: -1,
    is_licence:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    getCity({
      token: app.globalData.token
    }).then(res => {
      this.setData({
        province: res.data.data
      })
    })
    getProvinces({
      token: app.globalData.token
    }).then(res => {
      this.setData({
        province_two: res.data.data
      })
    })
    bankList({
      token: app.globalData.token
    }).then(res => {
      this.setData({
        totalBank: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  bindProvince: function(e) {
    var that = this
    that.setData({
      index: e.detail.value,
      merProvince: that.data.province[e.detail.value].value,
      city: that.data.province[e.detail.value].children,
      index2:-1,
      index3:-1,
      area: []
    })
  },
  bindCity: function(e) {
    var that = this
    that.setData({
      index2: e.detail.value,
      merCity: that.data.city[e.detail.value].value,
      area: that.data.city[e.detail.value].children,
      index3: -1
    })
  },
  bindArea: function(e) {
    var that = this
    console.log(that.data.area[e.detail.value])
    that.setData({
      index3: e.detail.value,
      merDistrict: that.data.area[e.detail.value].value,
    })
  },
  bindProvince_two: function(e) {
    var that = this
    that.setData({
      index_two: e.detail.value,
      bankProvince: that.data.province_two[e.detail.value].value,
      city_two: that.data.province_two[e.detail.value].children,
      index2_two: -1,
      totalBank_index: -1,
      branchBank: '',
      branchBank_index:-1
    })
  },
  bindCity_two: function(e) {
    var that = this
    that.setData({
      index2_two: e.detail.value,
      bankCity: that.data.city_two[e.detail.value].value,
      totalBank_index: -1,
      branchBank:'',
      branchBank_index: -1
    })
  },
  totalBank: function(e) {
    wx.showLoading({
      title: '正在获取信息',
      icon: 'loading'
    })
    var that = this
    if (that.data.index_two == -1 || that.data.index2_two == -1) {
      wx.showToast({
        title: '请先选择开户地址',
        duration: 2000,
        icon: 'none'
      })
      return;
    }
    var textnum = that.data.totalBank[e.detail.value].name + '股份有限公司'
    that.setData({
      totalBank_index: e.detail.value,
      headBankCode: that.data.totalBank[e.detail.value].code,
      branchBank_index: -1
    })
    bankBranchInfo({
      token: app.globalData.token,
      headBankCode: that.data.headBankCode,
      provinceCode: that.data.bankProvince,
      cityCode: that.data.bankCity
    }).then(res => {
      if (res.data.code == 666) {
        console.log(res)
        wx.hideLoading()
        console.log(res.data.data)
        if (res.data.data == '') {
          that.setData({
            branchBank: [{id:'',name:'暂无数据'}],
            branchBank_index: 0
          })
        } else {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].name = res.data.data[i].name.replace(textnum, '')
          }
          that.setData({
            branchBank: res.data.data
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  branchBank: function(e) {
    var that = this
    that.setData({
      branchBank_index: e.detail.value,
      bankCode: that.data.branchBank[e.detail.value].id,
    })
  },
  IDCARD_FRONT: function() {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.IDCARD_FRONT.url = res.data.data.url
              that.data.IDCARD_FRONT.thumb = res.data.data.thumb
              that.data.IDCARD_FRONT._file = res.data.data.base_image
              that.setData({
                IDCARD_FRONT: that.data.IDCARD_FRONT
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  IDCARD_BACK(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.IDCARD_BACK.url = res.data.data.url
              that.data.IDCARD_BACK.thumb = res.data.data.thumb
              that.data.IDCARD_BACK._file = res.data.data.base_image
              that.setData({
                IDCARD_BACK: that.data.IDCARD_BACK
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  SETTLE_BANKCARD(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.SETTLE_BANKCARD.url = res.data.data.url
              that.data.SETTLE_BANKCARD.thumb = res.data.data.thumb
              that.data.SETTLE_BANKCARD._file = res.data.data.base_image
              that.setData({
                SETTLE_BANKCARD: that.data.SETTLE_BANKCARD
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  HAND_IDCARD(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.setData({

              })
              that.data.HAND_IDCARD.url = res.data.data.url
              that.data.HAND_IDCARD.thumb = res.data.data.thumb
              that.data.HAND_IDCARD._file = res.data.data.base_image
              that.setData({
                HAND_IDCARD: that.data.HAND_IDCARD
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  HAND_BANKCARD(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.setData({

              })
              that.data.HAND_BANKCARD.url = res.data.data.url
              that.data.HAND_BANKCARD.thumb = res.data.data.thumb
              that.data.HAND_BANKCARD._file = res.data.data.base_image
              that.setData({
                HAND_BANKCARD: that.data.HAND_BANKCARD
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  UNI_CREDIT_CODE(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.setData({

              })
              that.data.UNI_CREDIT_CODE.url = res.data.data.url
              that.data.UNI_CREDIT_CODE.thumb = res.data.data.thumb
              that.data.UNI_CREDIT_CODE._file = res.data.data.base_image
              that.setData({
                UNI_CREDIT_CODE: that.data.UNI_CREDIT_CODE
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  formSubmit: function(e) {
    var that = this;
    var _aa = that.data
    var _form = e.detail.value
    var isPhone = /^1(3|4|5|7|8)\d{9}$/;
    var isok = isPhone.test(_form.merLegalPhone);
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    var iso = reg.test(_form.legalIdCard);
    if (!_form.legalIdCard) {
      wx.showToast({
        title: '请填写法人身份证号',
        icon: 'none'
      })
      return false;
    } else if (!_form.legalName) {
      wx.showToast({
        title: '请填写法人姓名',
        icon: 'none'
      })
      return false;
    } else if (!_form.merAddress) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      return false;
    } else if (_aa.index == -1 || _aa.index2 == -1 || _aa.index3 == -1) {
      wx.showToast({
        title: '请选择经营地址',
        icon: 'none'
      })
      return false;
    } else if (!_form.cardNo) {
      wx.showToast({
        title: '请填写银行卡号',
        icon: 'none'
      })
      return false;
    } else if (_aa.totalBank_index == -1 || _aa.branchBank_index == -1) {
      wx.showToast({
        title: '请选择开户银行',
        icon: 'none'
      })
      return false;
    } else if (_aa.index_two == -1 || _aa.index2_two == -1) {
      wx.showToast({
        title: '请选择开户银行地址',
        icon: 'none'
      })
      return false;
    } else if (!_aa.IDCARD_FRONT.thumb) {
      wx.showToast({
        title: '请添加身份证正面',
        icon: 'none'
      })
      return false;
    } else if (!_aa.IDCARD_BACK.thumb) {
      wx.showToast({
        title: '请添加身份证反面',
        icon: 'none'
      })
      return false;
    } else if (!_aa.SETTLE_BANKCARD.thumb) {
      wx.showToast({
        title: '请添加银行开照片',
        icon: 'none'
      })
      return false;
    } else if (!_aa.HAND_IDCARD.thumb) {
      wx.showToast({
        title: '请添加手持身份证图片',
        icon: 'none'
      })
      return false;
    } else if (!_aa.HAND_BANKCARD.thumb) {
      wx.showToast({
        title: '请添加手持银行卡图片',
        icon: 'none'
      })
      return false;
    } else if (!_form.merLegalPhone) {
      wx.showToast({
        title: '请添加手机号码',
        icon: 'none'
      })
      return false;
    } else if (!isok) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none'
      })
      return false;
    } else if (!iso) {
      wx.showToast({
        title: '身份证格式不正确',
        icon: 'none'
      })
      return false;
    }
    personalMan({
      token: app.globalData.token,
      legalIdCard: _form.legalIdCard, //法人身份证号
      legalName: _form.legalName, //法人姓名
      merAddress: _form.merAddress, //商户经营地址所在具体地址
      merCity: _aa.merCity, //商户经营地址所在市
      merDistrict: _aa.merDistrict, //商户经营地址所在区
      merProvince: _aa.merProvince, //商户经营地址所在省
      cardNo: _form.cardNo, //结算银行账号或者银行卡号
      headBankCode: _aa.headBankCode, //开户银行总行编码
      bankCity: _aa.bankCity, //开户市
      bankCode: _aa.bankCode, //开户银行支行编码
      bankProvince: _aa.bankProvince, //开户省
      merFullName: _aa.merFullName,
      fileInfo: {
        IDCARD_FRONT: _aa.IDCARD_FRONT, //法人身份证正面
        IDCARD_BACK: _aa.IDCARD_BACK, //法人身份证反面
        SETTLE_BANKCARD: _aa.SETTLE_BANKCARD, //结算银行卡
        HAND_IDCARD: _aa.HAND_IDCARD,
        HAND_BANKCARD: _aa.HAND_BANKCARD,
        UNI_CREDIT_CODE: _aa.UNI_CREDIT_CODE
      },
      merLegalPhone: _form.merLegalPhone, //商户联系人手机
      is_licence: _aa.is_licence,
      merShortName: _aa.merShortName
      // merLegalEmail: _form.merLegalEmail, //商户联系人邮箱
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        wx.showToast({
          title: '上传认证材料成功',
        })
        setTimeout(function() {
          wx.navigateTo({
            url: '/pages/is_auth/is_auth?is_auth=1',
          })
        }, 1500)
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
        return;
      }
    })
  },
  radioChange(e){
    this.setData({
      is_licence: e.detail.value
    })
  }
})